package com.adee.algorithm.graph;

class Constants {
    public static final int MAX_VEX = 6;
}

// 邻接矩阵 adjacency matrix 表示法，是一个方阵
// 1.创建矩阵类
class MGraph {
    char[]  vexs = new char[Constants.MAX_VEX];  // 顶点表
    int[][] arc = new int[Constants.MAX_VEX][Constants.MAX_VEX];  // 邻接矩阵，方阵。new基本类型数组，其元素会初始化为默认值

    // 2.初始化，将邻接矩阵初始化为全0
    public MGraph() {
        System.out.println("construct MGraph()");
        for (int i = 0; i < Constants.MAX_VEX; i++) {
            for (int j = 0; j < Constants.MAX_VEX; j++) {
                arc[i][j] = 0;
            }
        }
    }

    public MGraph(char[] vexs) {
        this();
        this.vexs = vexs;
    }

    // 3.根据给定的手写图，构造邻接矩阵，邻接矩阵中，每个元素表示一条边，设置每个边的值
    public void buildGraph() {
        arc[0][1] = 1;
        arc[0][2] = 1;
        arc[0][3] = 1;
    }

    // 获取给定顶点的出度，该方法针对无向图，无向图顶点的度即为该顶点连接的边的个数
    public int getOD(char point) {
        int pointIndex = -1;
        for (int i = 0; i < vexs.length; i++) {
            if (vexs[i] == point) {
                pointIndex = i;
                break;
            }
        }
        if (pointIndex == -1) {
            System.out.println("顶点[" + point + "]不存在.");
            return -1;
        }
        int od = 0;
        for (int i = 0; i < arc[pointIndex].length; i++) {
            od += arc[pointIndex][i];
        }
        return od;
    }

    public void print() {
        for (int i = 0; i < Constants.MAX_VEX; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < Constants.MAX_VEX; j++) {
                sb.append(arc[i][j]).append("\t");
            }
            System.out.println(sb.toString());

        }
    }
}
public class Test001_adjacency_matrix {


    public static void main(String[] args) {
        char[] vexs = {'a', 'b', 'c', 'd'};
        MGraph g = new MGraph(vexs);
        g.print();
    }


}
